package com.jesper.seckill.service.impl;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.code.starter.util.BeanUtil;
import com.code.starter.util.RedisUtils;
import com.jesper.seckill.constant.RedisKeyPrefix;
import com.jesper.seckill.dao.GoodsDao;
import com.jesper.seckill.dao.GoodsSeckillDao;
import com.jesper.seckill.entity.po.Order;
import com.jesper.seckill.entity.vo.request.PageBaseReq;
import com.jesper.seckill.entity.vo.response.PageBaseResp;
import com.jesper.seckill.entity.vo.response.goods.GoodsVO;
import com.jesper.seckill.entity.po.Goods;
import com.jesper.seckill.mapper.GoodsMapper;
import com.jesper.seckill.mapper.GoodsSeckillMapper;
import com.jesper.seckill.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjw
 * @since 2024-01-18
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsSeckillMapper goodsSeckillMapper;

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsSeckillDao goodsSeckillDao;

    @Override
    public PageBaseResp<GoodsVO> pageGoods(PageBaseReq request) {
        Page goodsPage = goodsDao.pageGoods(request.plusPage());
        List<GoodsVO> voList = BeanUtil.copyPropertiesByFiledNameOfList(goodsPage.getRecords(), GoodsVO.class);
        return PageBaseResp.init(goodsPage, voList);
    }

    @Override
    public List<GoodsVO> listGoods() {
        List<Goods> poList = goodsDao.getGoodsPOList();
        return BeanUtil.copyPropertiesByFiledNameOfList(poList,GoodsVO.class);
    }

    @Override
    public GoodsVO goodDetail(long goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        return BeanUtil.copyPropertiesByFiledName(goods,GoodsVO.class);
    }

    @Override
    @Transactional
    public void consumeSeckillMessage(String message) {
        if (StrUtil.isBlank(message)){
            return;
        }
        JSONObject jsonObject = JSONObject.parseObject(message);
        Long userId = jsonObject.getLong("userId");
        Long goodsId = jsonObject.getLong("goodsId");
        if (userId == null || goodsId == null){
            return;
        }
        //商品库存信息
        GoodsVO goodsVO = goodsSeckillMapper.getSeckillGoodsVOByGoodsId(goodsId);
        if (goodsVO == null || goodsVO.getStockCount() <= 0){
            return;
        }
        //判断重复秒杀
        Order order = RedisUtils.get(RedisKeyPrefix.getKey(RedisKeyPrefix.USER_SUCCESS_SEC_KILL_GOOD_INFO,userId, goodsId), Order.class);
        if (order != null){
            throw new ValidateException("不能重复秒杀");
        }
        //减库存 下订单 写入秒杀订单
        goodsSeckillDao.reduceStock(goodsId);

    }
}
